#include "register.h"
#include "widget.h"
#include "ui_register.h"
#include <QPainter>
#include <QDebug>
#include "QFile"
extern LinkList<Node>stu;

Register::Register(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    setWindowTitle("注册账号");
    ui->passward->setEchoMode(QLineEdit::Password);
    ui->passward_again->setEchoMode(QLineEdit::Password);
}

Register::~Register()
{
    delete ui;
}

void Register::on_registerButton_clicked()
{
    QString username=ui->username->text();
    QString passward=ui->passward->text();
    QString passward_again=ui->passward_again->text();
    QString invite=ui->invite->text();
    if(username==NULL)
    {
            QMessageBox::critical(this,"警告","无用户名，请重试！");
    }
    else if(passward==NULL || passward_again==NULL)
    {
        QMessageBox::critical(this,"警告","密码错误，请重试！");
    }
    else if(passward != passward_again)
    {
        QMessageBox::critical(this,"警告","前后密码不同，请重试！");
    }
    else if(invite==NULL)
    {
        QMessageBox::critical(this,"警告","无邀请码，请重试！");
    }
    else if(invite!="2020014322")
    {
        QMessageBox::critical(this,"警告","邀请码错误，请重试！");
    }
    else
    {
         string str_username,str_passward;

        str_passward=passward.toStdString();
        str_username=username.toStdString();


        QFile file(QCoreApplication::applicationDirPath()+"/date.txt");
        if(file.exists())
        {
           qDebug()<<"文件存在";
        }
        else{
            qDebug()<<"文件不存在,正在新建文件.";
            file.open( QIODevice::ReadWrite | QIODevice::Text );
            file.close();
        }
file.open(QIODevice::ReadOnly | QIODevice::Text);

        qDebug()<<"正在检查是否是否有匹配的id和密码";
        //创建文件读取流
        QTextStream in(&file);
        bool judge=true;;
        qDebug()<<"正在检查是否是否有匹配的id和密码";
        //遍历账号
        while(!in.atEnd())
        {

            in.setCodec("UTF-8");
            QString str= in.readLine();
            if(str=="\n")
             {
                 continue;
             }
            QStringList list = str.split(",");
            //账号
            QString dtn = list[0];
             if(dtn==username)
             {
                 qDebug()<<"已找到重复账号";
                 judge=false;
                 QMessageBox::critical(this,"警告","该账号已存在，请重试！");
                 break;
             }
        }
        file.close();

        if(judge)
        {
            file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
            QTextStream out(&file);
            out<<endl;
            out<<username<<","<<passward<<","<<endl;
            QMessageBox::information(this,"提示","账号创建成功！");
        }
        file.close();
    }
}

void Register::on_saveButton_clicked()
{
    Widget*a=new Widget;
    a->show();
    this->close();
}

void Register::paintEvent(QPaintEvent *)
{
    QPainter p(this);//创建画家对象p
    //绘图操作
    p.drawPixmap(rect(),QPixmap(":/new/prefix1/B1037CBC733E7C18C7288B8F72D8C49C.jpg"));
}


